package anthology.daily_question._24_10;

import java.util.Arrays;

//https://leetcode.cn/problems/smallest-range-ii/description/?envType=daily-question&envId=2024-10-21
public class _0910最小差值2 {
    class Solution {
        public int smallestRangeII(int[] A, int K) {
            int N = A.length;
            Arrays.sort(A);
            int ans = A[N-1] - A[0];

            for (int i = 0; i < A.length - 1; ++i) {
                int a = A[i], b = A[i+1];
                int high = Math.max(A[N-1] - K, a + K);
                int low = Math.min(A[0] + K, b - K);
                ans = Math.min(ans, high - low);
            }
            return ans;
        }
    }
}
